import { createApp } from "vue";

import ElementPlus from "element-plus";
import zhCn from "element-plus/es/locale/lang/zh-cn";

import "element-plus/dist/index.css";
import "@/assets/scss/theme.scss";

import * as ElementPlusIconsVue from "@element-plus/icons-vue";

import router from "./router";
import store from "./store";
import App from "./App.vue";

import { highlight, role, permits } from "./permission";

const app = createApp(App);

app.directive("highlight", highlight);
app.directive("role", role);
app.directive("permits", permits);

app
  .use(store)
  .use(ElementPlus, {
    locale: zhCn,
  });

  // 页面加载时检查是否有缓存菜单，并重新加载路由
const cachedState = sessionStorage.getItem('state')
if (cachedState) {
  const state = JSON.parse(cachedState)
  if (state.menus && state.menus.length > 0) {
    const { transformMenuToRoutes } = require('./utils/routerUtils')
    const routes = transformMenuToRoutes(state.menus)
    routes.forEach((route: any) => {
      router.addRoute('Layout', route)
    })
  }
}

app.use(router).mount("#app");

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
